package com.example.demo.controller;

import com.example.demo.data.ResultDate;
import com.example.demo.pojo.Role;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("user")
@Transactional  //开启事务回滚
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("list")
    public List<User> getUsers(){

        return userService.getAllUsers();

    }

    //多条件分页查询，获取用户和角色整合数据
    @GetMapping("pageInfoByName")
    public ResultDate<User> findInfoByName(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                           @RequestParam(value = "pageSize",defaultValue = "5") int pageSize,
                                           @RequestParam("trueName") String trueName){

        //获取用户角色信息
        PageInfo<User> userPageInfo = userService.findInfoByName(pageNum, pageSize, trueName);
        ResultDate<User> resultDate = new ResultDate<>();
        
        if (userPageInfo != null ){
            resultDate.setPageInfo(userPageInfo);
        }else {
            resultDate.setStatus(-1);
            resultDate.setMsg("无用户信息");
        }

        return resultDate;
    }

    //更新用户密码
    @PostMapping("updatePassword")
    public ResultDate<Object> updatePassword(HttpServletRequest request,
                                             @RequestParam("oldPassword") String oldPassword,
                                             @RequestParam("newPassword") String newPassword){
        if (oldPassword == null){
            oldPassword = "";
        }
        ResultDate<Object> resultDate = new ResultDate<>();
        //获取用户session信息
        User user = (User)request.getSession().getAttribute("user");
        String pw = user.getU_passWord();
        //开启套娃,比较输入框的密码和数据库中的密码是否一致
        if (oldPassword.equals(pw)){
            user.setU_passWord(newPassword);
            int userId = user.getU_id();

            //更新密码
            int flag = userService.updatePassword(user, userId);

            if (flag >= 1 ){
                resultDate.setMsg("密码更新成功~");
                resultDate.setObj(user);
                //更新后注销原session值
                request.getSession().invalidate();
            }else {
                resultDate.setStatus(-1);
                resultDate.setMsg("密码更新失败！");
            }
        }else {
            resultDate.setStatus(-1);
            resultDate.setMsg("原始密码输入不正确，请检查！");
        }

        return resultDate;
    }

    //角色列表信息
    @GetMapping("getRoleList")
    public ResultDate<Role> getRoleList(){

        List<Role> roleList = userService.getRoleList();
        ResultDate<Role> resultDate = new ResultDate<>();
        if (roleList !=null){
            resultDate.setList(roleList);
        }else {
            resultDate.setStatus(-1);
            resultDate.setMsg("服务端内部出错！");
        }
        return resultDate;
    }

    //添加用户信息
    @PostMapping("addUser")
    public ResultDate<Object> addUser(@RequestParam("loginName") String loginName,
                                      @RequestParam("passWord") String passWord,
                                      @RequestParam("trueName") String trueName,
                                      @RequestParam("email") String email,
                                      @RequestParam("state") int state,
                                      @RequestParam("roleId") int roleId){
        //新增用户信息
        User user = new User();
        user.setU_loginName(loginName);
        user.setU_passWord(passWord);
        user.setU_trueName(trueName);
        user.setU_email(email);
        user.setU_state(state);
        user.setR_id(roleId);

        int flag = userService.addUser(user);
        ResultDate<Object> resultDate = new ResultDate<>();
        if (flag >=1){
            resultDate.setObj(user);
            resultDate.setMsg("新增用户成功~");
        }else {
            resultDate.setStatus(-1);
            resultDate.setMsg("新增用户失败！");
        }

        return resultDate;
    }

    //通过登录名查询数据库是否存在
    @GetMapping("check")
    public ResultDate<User> check(@RequestParam("loginName") String loginName){

        List<User> users = userService.checkByloginName(loginName);
        ResultDate<User> resultDate = new ResultDate<>();
        if (users.size() >=1 ){
            resultDate.setMsg("登录名和其他人冲突，请重新输入！");
            resultDate.setList(users);
        }

        return resultDate;

    }

    //通过id获取用户信息
    @GetMapping("getInfoById")
    public ResultDate<Object> getInfoById(@RequestParam("id") int userId){

        List<Object> userInfo = userService.getUserInfoById(userId);
        ResultDate<Object> resultDate = new ResultDate<>();
        if (userInfo != null){
            resultDate.setList(userInfo);
        }else {
            resultDate.setStatus(-1);
            resultDate.setMsg("服务端出错！");
        }

        return resultDate;
    }

    //更新数据
    @PostMapping("update")
    public ResultDate<Object> updateById(@RequestParam("userId") int userId,
                                         @RequestParam("loginName") String loginName,
                                         @RequestParam("realName") String realName,
                                         @RequestParam("email") String email,
                                         @RequestParam("state") int state,
                                         @RequestParam("roleId") int roleId){

        User user = new User();
        user.setU_loginName(loginName);
        user.setU_trueName(realName);
        user.setU_email(email);
        user.setU_state(state);
        user.setR_id(roleId);

        int flag = userService.updateUser(userId,user);
        ResultDate<Object> resultDate = new ResultDate<>();
        if (flag >=1 ){
            resultDate.setMsg("用户数据更新成功~");
        }else {
            resultDate.setStatus(-1);
            resultDate.setMsg("服务端出错！");
        }
        return resultDate;
    }

    //删除数据
    @PostMapping("delete")
    public ResultDate<Object> delete(@RequestParam("userId") int userId){

        int flag = userService.delete(userId);
        ResultDate<Object> resultDate = new ResultDate<>();
        if (flag >=1 ){
            resultDate.setMsg("删除成功~");
        }else {
            resultDate.setStatus(-1);
            resultDate.setMsg("服务器出错！");
        }

        return resultDate;
    }

    //通过roleid获取角色信息
    @GetMapping("getRoleInfo")
    public Role getRoleInfo(@RequestParam("roleId") int roleId){

        return userService.getRoleInfoByRoleId(roleId);

    }

}
